package com.ebid.templates.ten;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TenderProjectPhase {

    public static String tenderProjectPhase = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            +"<tenderProjectPhase> \n" +
            "    <PROJECT> \n" +
            "      <PROJECT_CODE>{0}</PROJECT_CODE>\n" +
            "      <PROJECT_NAME>{1}</PROJECT_NAME>\n" +
            "      <INVEST_PROJECT_CODE>{2}</INVEST_PROJECT_CODE>\n" +
            "      <REGION_CODE>{3}</REGION_CODE>\n" +
            "      <INDUSTRIES_TYPE>{4}</INDUSTRIES_TYPE>\n" +
            "      <FUND_SOURCE>{5}</FUND_SOURCE>\n" +
            "      <PROJECT_SCALE>{6}</PROJECT_SCALE>\n" +
            "    </PROJECT>\n" +
            "    <TENDER_PROJECT_CODE>{7}</TENDER_PROJECT_CODE>\n" +
            "    <TENDER_PROJECT_NAME>{8}</TENDER_PROJECT_NAME>\n" +
            "    <TENDER_PROJECT_INDUSTRIES_TYPE>{9}</TENDER_PROJECT_INDUSTRIES_TYPE>\n" +
            "    <REGION_CODE>{10}</REGION_CODE>\n" +
            "    <TENDER_CONTENT>{11}</TENDER_CONTENT>\n" +
            "    <TENDERERS> \n" +
            "       {12}"+
            "    </TENDERERS>\n" +
            "    <TENDER_AGENCYS> \n" +
            "      {13}"+
            "    </TENDER_AGENCYS>\n" +
            "    <TENDER_MODE>{14}</TENDER_MODE>\n" +
            "    <TENDER_ORGANIZE_FORM>{15}</TENDER_ORGANIZE_FORM>\n" +
            "    <CREATE_TIME>{16}</CREATE_TIME>\n" +
            "    <SUPERVISE_DEPT_CODE>{17}</SUPERVISE_DEPT_CODE>\n" +
            "    <SUPERVISE_DEPT_NAME>{18}</SUPERVISE_DEPT_NAME>\n" +
            "    <APPROVE_DEPT_CODE>{19}</APPROVE_DEPT_CODE>\n" +
            "    <APPROVE_DEPT_NAME>{20}</APPROVE_DEPT_NAME>\n" +
            "    <REPORT_DUTY>{21}</REPORT_DUTY>\n" +
            "    <BID_SECTIONS> \n" +
            "       {22}"+
            "    </BID_SECTIONS>\n" +
            "    <ATTACHMENT_SET> \n" +
            "       {23}"+
            "    </ATTACHMENT_SET>\n" +
            "    <TRADE_PLAT>{24}</TRADE_PLAT>\n" +
            "    <PUB_SERVICE_PLAT_CODE>{25}</PUB_SERVICE_PLAT_CODE>\n" +
            "    <SUBMIT_TIMESTAMP>{26}</SUBMIT_TIMESTAMP>\n" +
            "  </tenderProjectPhase>";

    private static StringBuilder TENDERERS = new StringBuilder();
    private static String TENDERER =
            "<TENDERER> \n" +
            "        <TENDER_CODE_TYPE>{0}</TENDER_CODE_TYPE>\n" +
            "        <TENDERER_CODE>{1}</TENDERER_CODE>\n" +
            "        <TENDERER_NAME>{2}</TENDERER_NAME>\n" +
            "      </TENDERER>\n";

    private static StringBuilder TENDER_AGENCYS = new StringBuilder();
    private static String TENDER_AGENCY="\t<TENDER_AGENCY>\n" +
            "           <TENDER_AGENCY_CODE_TYPE>{0}</TENDER_AGENCY_CODE_TYPE>\n" +
            "           <TENDER_AGENCY_CODE>{1}</TENDER_AGENCY_CODE>\n" +
            "           <TENDER_AGENCY_NAME>{2}</TENDER_AGENCY_NAME>\n" +
            "       </TENDER_AGENCY>\n";

    private static StringBuilder BID_SECTIONS = new StringBuilder();
    private static String BID_SECTION = "<BID_SECTION> \n" +
            "           <BID_SECTION_CODE>{0}</BID_SECTION_CODE>\n" +
            "           <BID_SECTION_NAME>{1}</BID_SECTION_NAME>\n" +
            "           <BID_SECTION_CONTENT>{2}</BID_SECTION_CONTENT>\n" +
            "           <BID_SECTION_CLASSIFY_CODE>{3}</BID_SECTION_CLASSIFY_CODE>\n" +
            "           <CONTRACT_RECKON_PRICE>{4}</CONTRACT_RECKON_PRICE>\n" +
            "           <CURRENCY_CODE>{5}</CURRENCY_CODE>\n" +
            "           <BIDDING_ELIGIBILITY>{6}</BIDDING_ELIGIBILITY>\n" +
            "       </BID_SECTION>\n";


    private static StringBuilder ATTACHMENTS = new StringBuilder();
    public static String ATTACHMENT = "\t<ATTACHMENT>\n" +
            "            <ATTACHMENT_CODE>{0}</ATTACHMENT_CODE>\n" +
            "            <ATTACHMENT_FILE_NAME>{1}</ATTACHMENT_FILE_NAME>\n" +
            "            <FILE_TYPE>{2}</FILE_TYPE>\n" +
            "            <DATA_TIMESTAMP>{3}</DATA_TIMESTAMP>\n" +
            "            <MD5>{4}</MD5>\n" +
            "        </ATTACHMENT>\n";

    public static void setTenderProjectPhase(String tenderProjectPhase,String s0, String s1, String s2, String s3, String s4, String s5, String s6, String s7, String s8, String s9, String s10,
                                             String s11, String s12, String s13, String s14, String s15, String s16, String s17, String s18, String s19, String s20,
                                             String s21, String s22, String s23, String s24, String s25, String s26) {
        TenderProjectPhase.tenderProjectPhase = MessageFormat.format(tenderProjectPhase,s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,
                s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24,s25,s26);
    }

    public static void setTENDERER(String TENDERER, List<Map> lists) {

        if (lists == null){
            return;
        }
        for (Map<String,String> map : lists) {
            String args[] = new String[3];
            int i=0;
            for(Map.Entry<String,String> entry : map.entrySet()){
                args[i++] = entry.getValue();
            }
            TENDERER = MessageFormat.format(TENDERER,args);
            TENDERERS.append(TENDERER);
        }
    }

    public static void setBidSection(String bidSectionCode, List<Map> lists) {

        if (lists == null)
            return;
        for (Map<String,String> map : lists) {
            String args[] = new String[7];
            int i=0;
            for(Map.Entry<String,String> entry : map.entrySet()){
                args[i++] = entry.getValue();
            }
            BID_SECTION = MessageFormat.format(BID_SECTION,args);
            BID_SECTIONS.append(BID_SECTION);
        }

    }

    public static void setTenderAgency(String tenderAgency,List<Map> lists) {
        if (lists == null){
            return;
        }
        for (Map<String,String> map : lists) {
            String args[] = new String[3];
            int i=0;
            for(Map.Entry<String,String> entry : map.entrySet()){
                args[i++] = entry.getValue();
            }
            TENDER_AGENCY = MessageFormat.format(TENDER_AGENCY,args);
            TENDER_AGENCYS.append(TENDER_AGENCY);
        }
    }


    public static void setATTACHMENT(String ATTACHMENT,List<Map> lists) {
        if (lists == null)
            return;

        for (Map<String,String> map : lists) {
            String args[] = new String[5];
            int i=0;
            for(Map.Entry<String,String> entry : map.entrySet()){
                args[i++] = entry.getValue();
            }
            ATTACHMENT = MessageFormat.format(ATTACHMENT,args);
            ATTACHMENTS.append(ATTACHMENT);
        }
    }

    public static void createTenderProjectPhase(){

        setTENDERER(TENDERER,TenderProjectPhaseUtils.getList("TENDERERS"));
        setTenderAgency(TENDER_AGENCY,TenderProjectPhaseUtils.getList("TENDER_AGENCYS"));
        setBidSection(BID_SECTION,TenderProjectPhaseUtils.getList("BID_SECTIONS"));
        setATTACHMENT(ATTACHMENT,TenderProjectPhaseUtils.getList("ATTACHMENTS"));
        setTenderProjectPhase(tenderProjectPhase,
                TenderProjectPhaseUtils.get("PROJECT_CODE"),
                TenderProjectPhaseUtils.get("PROJECT_NAME"),
                TenderProjectPhaseUtils.get("INVEST_PROJECT_CODE"),
                TenderProjectPhaseUtils.get("REGION_CODE"),
                TenderProjectPhaseUtils.get("INDUSTRIES_TYPE"),
                TenderProjectPhaseUtils.get("FUND_SOURCE"),
                TenderProjectPhaseUtils.get("PROJECT_SCALE"),
                TenderProjectPhaseUtils.get("TENDER_PROJECT_CODE"),
                TenderProjectPhaseUtils.get("TENDER_PROJECT_NAME"),
                TenderProjectPhaseUtils.get("TENDER_PROJECT_INDUSTRIES_TYPE"),
                TenderProjectPhaseUtils.get("REGION_CODE"),
                TenderProjectPhaseUtils.get("TENDER_CONTENT"),
                TENDERERS.toString(),
                TENDER_AGENCYS.toString(),
                TenderProjectPhaseUtils.get("TENDER_MODE"),
                TenderProjectPhaseUtils.get("TENDER_ORGANIZE_FORM"),
                TenderProjectPhaseUtils.get("CREATE_TIME"),
                TenderProjectPhaseUtils.get("SUPERVISE_DEPT_CODE"),
                TenderProjectPhaseUtils.get("SUPERVISE_DEPT_NAME"),
                TenderProjectPhaseUtils.get("APPROVE_DEPT_CODE"),
                TenderProjectPhaseUtils.get("APPROVE_DEPT_NAME"),
                TenderProjectPhaseUtils.get("REPORT_DUTY"),
                BID_SECTIONS.toString(),
                ATTACHMENTS.toString(),
                TenderProjectPhaseUtils.get("TRADE_PLAT"),
                TenderProjectPhaseUtils.get("PUB_SERVICE_PLAT_CODE"),
                TenderProjectPhaseUtils.get("SUBMIT_TIMESTAMP")
                );
    }

    public static void main(String[] args) {
      /*  List list = new ArrayList();
        Map tender = new HashMap();
        tender.put("1","1");
        tender.put("2","2");
        tender.put("3","3");
        list.add(tender);
        Map tenderAgency = new HashMap();
        tenderAgency.put("1","1");
        tenderAgency.put("2","2");
        tenderAgency.put("3","3");

        List attas = new ArrayList();
        Map atta = new HashMap();
        atta.put("1","1");
        atta.put("2","2");
        atta.put("3","3");
        atta.put("4","4");
        atta.put("5","5");
        attas.add(atta);

        setTENDERER(TENDERER,list);
        setTenderAgency(TENDER_AGENCY,list);
        setBidSection(BID_SECTION,list);
        setATTACHMENT(ATTACHMENT,attas);
        setTenderProjectPhase(tenderProjectPhase,
                                TenderProjectPhaseUtils.get("PROJECT_CODE"),
                                TenderProjectPhaseUtils.get("PROJECT_NAME"),
                                TenderProjectPhaseUtils.get("INVEST_PROJECT_CODE"),
                                TenderProjectPhaseUtils.get("REGION_CODE"),
                                TenderProjectPhaseUtils.get("INDUSTRIES_TYPE"),
                                TenderProjectPhaseUtils.get("FUND_SOURCE"),
                                TenderProjectPhaseUtils.get("PROJECT_SCALE"),
                                TenderProjectPhaseUtils.get("TENDER_PROJECT_CODE"),
                                TenderProjectPhaseUtils.get("TENDER_PROJECT_NAME"),
                                TenderProjectPhaseUtils.get("TENDER_PROJECT_INDUSTRIES_TYPE"),
                                TenderProjectPhaseUtils.get("REGION_CODE"),
                                TenderProjectPhaseUtils.get("TENDER_CONTENT"),
                                TENDERERS.toString(),
                                TENDER_AGENCYS.toString(),
                                TenderProjectPhaseUtils.get("TENDER_MODE"),
                                TenderProjectPhaseUtils.get("TENDER_ORGANIZE_FORM"),
                                TenderProjectPhaseUtils.get("CREATE_TIME"),
                                TenderProjectPhaseUtils.get("SUPERVISE_DEPT_CODE"),
                                TenderProjectPhaseUtils.get("SUPERVISE_DEPT_NAME"),
                                TenderProjectPhaseUtils.get("APPROVE_DEPT_CODE"),
                                TenderProjectPhaseUtils.get("APPROVE_DEPT_NAME"),
                                TenderProjectPhaseUtils.get("REPORT_DUTY"),
                                BID_SECTIONS.toString(),
                                ATTACHMENTS.toString(),
                                TenderProjectPhaseUtils.get("TRADE_PLAT"),
                                TenderProjectPhaseUtils.get("PUB_SERVICE_PLAT_CODE"),
                                TenderProjectPhaseUtils.get("SUBMIT_TIMESTAMP")
        );*/

      createTenderProjectPhase();
        System.out.println(tenderProjectPhase);
    }
}
